module namespace infoview = "http://www.xmlproject.com/smi/lib/view/infoview";

import module namespace http ="http://www.28msec.com/modules/http";
import module namespace xqddf = "http://www.zorba-xquery.com/modules/xqddf";


declare namespace eBayNs="urn:ebay:apis:eBLBaseComponents";

declare collection infoview:entries as node()*;
declare variable $infoview:entries as xs:QName := xs:QName("infoview:entries");

declare sequential function infoview:showdetails($title as xs:string,
                                      $imdbResults as element(searchresult),
                                      $theMovieDbResults as element(searchresult),
                                      $rotenTomatoesResults as element(searchresult),
                                      $youtubeResults as element(searchresult) ) as element()
{
let $rtRendering := infoview:renderSearchResultRotenTomatoes($rotenTomatoesResults)
let $imdbRendering := infoview:renderSearchResultImdb($imdbResults)
let $tmdbRendering :=infoview:renderSearchResultTmdb($theMovieDbResults)
let $insert:= infoview:addSearch($title, string($theMovieDbResults/movie/cover/@url))
return
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html" charset="utf-8" />
                    <meta name="description" content="" />
                    <meta name="keywords" content="" />
                    <title>Movie Info</title>
                    <link href="/css/style.css" rel="stylesheet" type="text/css" />
                </head>
                <body>
                    <div id="wrap">
                        <div id="header" align="left">
                        </div>
                        
                        <div id="menu">
                            <a href="../moviesearch/index.xq" class="selected">Home Page</a>
                            <a href="../moviesearch/aboutus.xq" class="selected">About us</a>
                        </div>
                        
                        <div id="body">
                            <div id="top" align="center">
                                <h1>Movie Info</h1>
                                <form id="search-form" action="/merchandising/showItems" method="GET">
                                    <p>
                                    <input type="text" size="60" name="query" value="" />
                                    <input type="hidden" name="title" value="{$title}" />
                                    <input type="submit" value="Search Merchandising" />
                                    </p>
                                </form>
                                <hr/>
                            </div>
                            <div id="content" class="info">
                                <div id="left">
                                    <center><h2>{$rtRendering}</h2></center>
                                    {
                                         $imdbRendering;
                                    } 
                                    <p>
                                        {
                                            $tmdbRendering;
                                        }
                                    </p>
                                </div>
                                <div id="right">
                                    <h2>Trailers on Youtube</h2>
                                    {
                                            infoview:renderSearchResultYoutube($youtubeResults);
                                     }
                                </div>
                            </div>
                            <div>
                            {infoview:list()}
                            </div>
                            <div id="footer"> Project of XML and Databases :: Master in Applied Computer Science, University of Freiburg 2012 </div>
                        </div>
                        
                        <div id="bottom">
                        </div>
                    </div>
                </body>
        </html>
};

declare function infoview:renderSearchResultImdb($imdbResults as element(searchresult)) as element()
{
    let $r :=
       <table>
        <tr>
            <td class="highlight">Gender:</td><td>{$imdbResults/movie/genre}</td>
        </tr>
        <tr>
            <td class="highlight">Director:</td><td>{$imdbResults/movie/director}</td>
        </tr>
        <tr>
            <td class="highlight">Writers:</td><td>{$imdbResults/movie/writer}</td>
        </tr>
        <tr>
            <td class="highlight">Actors:</td><td>{$imdbResults/movie/actors}</td>
        </tr>
       </table>
       return $r
};

declare function infoview:renderSearchResultTmdb($theMovieDbResults as element(searchresult)) as element()
{
    let $r :=
        <div>
       <p>{$theMovieDbResults/movie/overview}</p>
       
       <img src="{$theMovieDbResults/movie/cover/@url}" alt="movie image"></img>
       </div>
       return $r
};

declare function infoview:renderSearchResultYoutube($youtubeResults as element(searchresult)) as element()
{

<table>{
    for $result in $youtubeResults/result
        let $title := data($result/@title)
        let $href := data($result/@href)
        (: get rid of all line feeds and carriage returns :)
        let $content := normalize-space($result/text()) 
    return
        
            <tr style="vertical-align: top;">
                <td style="width:50%">
                <h3><a href="{$href}">{$title}</a></h3>
                </td>
            </tr>;}
</table>
};

declare function infoview:renderSearchResultRotenTomatoes($rotenTomatoesResults as element(searchresult)) as element()
{
    let $r :=
       <p>{$rotenTomatoesResults/movie/title/text()}</p>
       
       return $r
};


declare sequential function infoview:addSearch($title, $url)
{
  http:set-header("Cache-Control", "no-cache");
  let $entries := xqddf:collection($infoview:entries)
  return 
      if ($entries//@title != $title)
      then
      xqddf:insert-nodes(
          $infoview:entries,
          <entry date="{fn:current-date()}" time="{fn:current-time()}" title="{$title}" posterUrl="{$url}">
          </entry>
      )
      else();
};

declare function infoview:list() {
  
  let $entries := for $e in xqddf:collection($infoview:entries)
                  order by xs:date($e/@date), xs:time($e/@time)
                  return $e
  let $num_entries := fn:count($entries)
  return
    if($num_entries = 0)
    then    
      <p><b>No entries, yet.</b></p>
    else
      <p>
      {
        for $entry at $pos in $entries[position() gt $num_entries - 4]
        let $numbering := if ($num_entries lt 4) then $pos else $num_entries - ( 4 - $pos) 
        return <p><p>{string($entry/@title)}</p>
               <img src="{string($entry/@posterUrl)}" width="125" height="178"/></p>
      }
      </p>;
};